package github.fafaovo.common.guiblock.containers;

import github.fafaovo.common.guiblock.MyIntArray;
import github.fafaovo.common.guiblock.tileentity.ExampleBurnMachineTileEntity;
import github.fafaovo.common.guiblock.tileentity.ExampleInitialEntity;
import github.fafaovo.core.init.ContainerInit;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraft.util.IIntArray;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

//服务兼客户端 建立物品槽位
public class ExampleInitialContainers extends Container {

    private MyIntArray intArray;

    private final LazyOptional<IItemHandler> UP;

    public ExampleInitialContainers(int id, PlayerInventory playerInventory, BlockPos pos, World world, MyIntArray intArray) {
        super(ContainerInit.INITIAL_CONTAINERS.get(), id);//todo
        //同步数据的数组
        this.intArray = intArray;
        trackIntArray(this.intArray);

        ExampleInitialEntity tileEntity = (ExampleInitialEntity) world.getTileEntity(pos);//todo
        //一个Inventory里可以有很多个Slot，这里我们需要给他添加进我们的GUI中，
        // 其中Slot第一个参数是一个Inventory，第二个参数是Inventory中ItemStack的序号，
        // 因为我们的Inventory总数只有1，所以这里填入0即可。
        // 最后两个坐标是X，Y。这个X，Y的起始点是( (this.width - this.xSize) / 2,
        // (this.height - this.ySize) / 2)，这里的xSize和ySize我们在之后将Screen的时候会讲到。


        Capability<IItemHandler> IHCap = CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
        this.UP = tileEntity.getCapability(IHCap, Direction.UP);


        this.UP.ifPresent((s)->{
            //80 32 槽的位置
            this.addSlot(new SlotItemHandler(s,0,80,32));
        });


        layoutPlayerInventorySlots(playerInventory, 8, 84);
    }

    @Override
    public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
        return ItemStack.EMPTY;
    }

    private int addSlotRange(IInventory inventory, int index, int x, int y, int amount, int dx) {
        for (int i = 0; i < amount; i++) {
            addSlot(new Slot(inventory, index, x, y));
            x += dx;
            index++;
        }
        return index;
    }

    private int addSlotBox(IInventory inventory, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
        for (int j = 0; j < verAmount; j++) {
            index = addSlotRange(inventory, index, x, y, horAmount, dx);
            y += dy;
        }
        return index;
    }

    private void layoutPlayerInventorySlots(IInventory inventory, int leftCol, int topRow) {
        // Player inventory
        addSlotBox(inventory, 9, leftCol, topRow, 9, 18, 3, 18);

        // Hotbar
        topRow += 58;
        addSlotRange(inventory, 0, leftCol, topRow, 9, 18);
    }


    public IIntArray getIntArray() {
        return intArray;
    }


    //玩家打开行为
    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return true;
    }

}
